package com.yjps.house.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yjps.common.message.Message;
import com.yjps.common.model.Address;
import com.yjps.common.page.Pager;
import com.yjps.common.util.TimeUtil;
import com.yjps.house.model.Building;
import com.yjps.house.model.House;
import com.yjps.house.service.BuildingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Jelly on 2015/8/25 0025.
 */

@Controller
@RequestMapping("/building")
public class BuildingController {

    @Autowired
    BuildingService buildingService;



    /**
     * 获取楼盘信息
     *
     * @param agentsId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list.do", method = RequestMethod.GET)
    public String getList(@RequestParam(value = "agentsId", required = true) String agentsId,HttpServletResponse response) {
        Message message;
        int agentsID = Integer.parseInt(agentsId);
        List<Building> buildingsList = buildingService.getList(agentsID);

        if(!buildingsList.isEmpty()){
            //String jsonString = JSON.toJSONString(buildingsList);
            //System.out.println(jsonString);
            //Map<String,Object> map=new HashMap<>();
            //map.put("buildingsList",buildingsList);
            for(Building b:buildingsList){
                Address address = buildingService.findAddress(b.getAddressId());
                if(address!=null)
                    b.setAddress(address.getProvince()+address.getCity()+address.getCounty()+address.getTown()+address.getDetail());
            }
            message = new Message(true,"成功获取楼盘列表",buildingsList);
        }else {
            message = new Message(false,"nothing");
        }
        //response.setHeader("Access-Control-Allow-Origin","*");

        return message.toJson();
    }


    /**
     * 根据门店经纪人id获取楼盘列表
     *
     * @param agentsId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/listByAgent.do", method = RequestMethod.GET)
    public String getListByAgent(@RequestParam(value = "agentsId", required = true) String agentsId,HttpServletResponse response) {
        Message message;
        int agentsID = Integer.parseInt(agentsId);
        List<Building> buildingsList = buildingService.getListByAgent(agentsID);

        if(!buildingsList.isEmpty()){

            for(Building b:buildingsList){
                Address address = buildingService.findAddress(b.getAddressId());
                if(address!=null)
                    b.setAddress(address.getProvince()+address.getCity()+address.getCounty()+address.getTown()+address.getDetail());
            }
            message = new Message(true,"成功获取jj人楼盘列表",buildingsList);
        }else {
            message = new Message(false,"获取jj人楼盘列表失败");
        }
        //response.setHeader("Access-Control-Allow-Origin","*");

        return message.toJson();
    }


    /**
     * 删除楼盘信息
     *
     * @param  buildingId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete.do", method = RequestMethod.GET)
    public String delete(@RequestParam(value = "buildingId", required = true) String buildingId) {
        int buildingID=Integer.parseInt(buildingId);
        Message message;
        if (buildingID<=0){
            message=new Message(false,"参数错误");
        }else{
            buildingService.delete(buildingID);
            message=new Message(true,"删除楼盘成功");
        }
        return message.toJson();
    }

    /**
     * 添加楼盘信息
     *
     * @param json { "address": { "province": "", "city": "", "country": "","town": "", "detail": "" } , "building": { "agentsId": "", "title": "", "description": "", "imageUrl": "", "averagePrice": "", "houseType": "" }}
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/add.do", method = RequestMethod.POST)
    public String add(@RequestParam(value = "json") String json) {
        //解析json
        JSONObject object = JSON.parseObject(json);

        Address address0 = JSON.parseObject(object.getString("address"),Address.class);
        int addressId = buildingService.addAddress(address0);

        //int agentsId = Integer.parseInt(object.getString("agentsId"));

        //String time = TimeUtil.getCurrentTimeInString(TimeUtil.DATE_FORMAT_DATE);
        Building building0 = JSON.parseObject(object.getString("building"), Building.class);

        //调用业务
        int buildingId = buildingService.add(building0,addressId);

        //封装返回
        Message message;
        Map<String,Object> map=new HashMap<>();
        if (buildingId>0){
            map.put("buildingId",buildingId);
            message=new Message(true,"添加楼盘成功",map);
        }else {
            message=new Message(false,"添加楼盘失败");
        }
        return message.toJson();
    }

    /**
     * 修改楼盘信息
     *
     * @param json { "id":"",  "title": "", "description": "", "imageUrl": "", "averagePrice": "", "houseType": "" }
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/update.do", method = RequestMethod.POST)
    public String update(@RequestParam(value = "json") String json) {
        //解析json

        JSONObject object = JSON.parseObject(json);
        int buildingId=Integer.parseInt(object.getString("id"));
        String title=object.getString("title");
        String description=object.getString("description");
        String imageUrl=object.getString("imageUrl");
        int averagePrice=Integer.parseInt(object.getString("averagePrice"));
        String houseType=object.getString("houseType");


        //调用业务
        boolean flag = false;
        flag = buildingService.update(buildingId,title,description,imageUrl,averagePrice,houseType);


        //封装返回
        Message message;
        if (flag) {
            message = new Message(flag, "更新操作成功");
        } else {
            message = new Message(flag, "更新失败");
        }
        return message.toJson();
    }

    /**
     * 条件查询楼盘
     * @param city
     * @param averagePrice
     * @param houseType
     * @param pageNo
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/query.do",method = RequestMethod.POST)
    @ResponseBody
    public String queryList(@RequestParam(value = "city") String city,
                            @RequestParam(value = "averagePrice")   String averagePrice,
                            @RequestParam(value = "houseType")   String houseType,
                            @RequestParam(value = "pageNo")   String pageNo,
                            HttpServletRequest request,HttpServletResponse response){

        //调用业务
        Pager<Building> buildingList=buildingService.queryBuildingByPager(new Pager<Building>(Integer.parseInt(pageNo),5), city, averagePrice, houseType);
        List<Building> buildings = buildingList.getList();
        //封装
        Message message;
        if (buildings!=null&&!buildings.isEmpty()){
            for(Building b:buildings){
                Address address = buildingService.findAddress(b.getAddressId());
                if(address!=null)
                    b.setAddress(address.getProvince()+address.getCity()+address.getCounty()+address.getTown()+address.getDetail());
            }
            message=new Message(true,"query success",buildingList);
        }else{
            message=new Message(false,"query nothing");
        }
        response.setHeader("Access-Control-Allow-Origin","*");
        return message.toJson();
    }

}
